package com.int_tech.car.service;

import com.int_tech.car.dao.CarDao;
import com.int_tech.car.entity.Car;
import com.int_tech.utils.CarEnum;
import com.int_tech.utils.Result;
import org.hibernate.engine.jdbc.Size;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/9/22.
 */
@Service
public class CarService {

    @Autowired
    private CarDao carDao;

    @Value("${size}")
    private int size;

    /**
     * TODO 添加啊车辆
     *
     * @param car
     * @return Result
     * @author dmy
     */
    public Result saveCar(Car car) {
        try {
            //根据车牌号验证车辆是否存在
            if (carDao.findCarByCarNumberAndIsDel(car.getCarNumber(), CarEnum.ON.getCode())!=null){
                return Result.build(201,"车牌号已存在");
                }else {
                car.setCreateTime(new Date());
                car.setUpdateTime(new Date());
                carDao.save(car);
                return Result.ok();
            }
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 修改车辆信息
    * @param car
    * @return Result
    * @author dmy
    */
    public Result updateCar(Car car){
        try {
            car.setUpdateTime(new Date());
            carDao.save(car);
            return Result.ok();
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据车辆id查询车辆信息
    * @param id
    * @return Result(Car)
    * @author dmy
    */
    public Result findCarById(Integer id){
        try {
            return Result.ok(carDao.findOne(id));
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
        	return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据id逻辑删除车辆信息
    * @param id
    * @return Result
    * @author dmy
    */
    @Transactional
    public Result deleteCarByid(Integer id){
        try {
             carDao.save(carDao.findOne(id).setIsDel(CarEnum.OFF.getCode()));
             return Result.ok();
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 分页查询车辆信息
    * @param  currentPage 当前页
    * @return Result(list<Car>)
    * @author dmy
    */
    public Result fingByPage(int currentPage){
        try {
            PageRequest pageRequest = new PageRequest(currentPage-1,size);
        		return Result.ok(carDao.findCarByIsDel(pageRequest,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 查询所有车辆信息
    * @return Result(list<Car>)
    * @author dmy
    */
    public Result findAllCar(){
        try {
        	return Result.ok(carDao.findCarByIsDel(CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据车牌号查询车辆信息
    * @param carNumber
    * @return Result(Car)
    * @author dmy
    */
    public Result findCarByCarNumber(String carNumber){
        try {
            return Result.ok(carDao.findCarByCarNumberAndIsDel(carNumber,CarEnum.ON.getCode()));
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据场站id查询车辆信息
    * @param stationId
    * @return Result(list<Car>)
    * @author dmy
    */
    public Result findCarByStationId(Integer stationId){
        try {
        		return Result.ok(carDao.findCarByStationIdAndIsDel(stationId,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }
    /**
    * TODO 根据客户id场站id查询车辆信息
    * @param stationId,clientId
    * @return Result(list<Car>)
    * @author dmy
    */
    public Result findCarByStationIdAndClientId(int stationId,int clientId){
        try {
        	return Result.ok(carDao.findCarByStationIdAndClientIdAndIsDel(stationId,clientId,CarEnum.ON.getCode()));
        	} catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    /**
    * TODO 根据客户id查询车辆信息
    * @param clientId
    * @return Result(list<Car>)
    * @author dmy
    */
    public  Result findCarByClientId(int clientId){
        try {
            List<Car> carList = carDao.findCarByClientIdAndIsDel(clientId, CarEnum.ON.getCode());
            return Result.ok(carList);
        } catch (Exception e) {
        	// TODO: handle exception
        	e.printStackTrace();
            return Result.build(500,"异常");
        }
    }

    public List<Car> findCarsByStationId(Long stationId) {
        String sid = String.valueOf(stationId);
        return carDao.findCarByStationIdAndIsDel(Integer.valueOf(sid),0);
    }
    /**
    * TODO 分页
    * @param pageable
    * @return
    * @author dmy
    */
    public Page<Car> getCarByPageable(Pageable pageable,int clientId){
        Page<Car> carPage = carDao.findByClientIdAndIsDel(pageable, clientId, CarEnum.ON.getCode());
        return carPage;
    }

    /**
     * TODO 分页
     * @param pageable
     * @return
     * @author dmy
     */
    public Page<Car> getCarByPageable(Pageable pageable){
        Page<Car> carPage = carDao.findByIsDel(pageable,CarEnum.ON.getCode());
        return carPage;
    }

    public Page<Car> getCarByPageableByStationId(Pageable pageable, String stationId) {

        Page<Car> carPage = carDao.findByStationIdAndIsDel(pageable,Integer.parseInt(stationId),0);
        return carPage;
    }

    public Page<Car> getCarByPageableByStationIdIn(Pageable pageRequest, List<Integer> sids) {
        Page<Car> carPage = carDao.findByStationIdInAndIsDel(pageRequest,sids,0);
        return carPage;
    }
}
